﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using InstrumentCheck.Module;
using InstrumentCheck.BLL;
using System.Collections.Generic;
namespace InstrumentCheck.WebUI
{
    public partial class CorrectQuery : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDropDownList();
                //InitGridView();
            }
        }
        protected void InitDropDownList()
        {
            List<User> source = new List<User>();
            source = CorrectQuery_bll.getCorrectDept();
           
            ddlKeepDept.DataTextField = "DeptNO";
            ddlKeepDept.DataValueField = "DeptNO";
            ddlKeepDept.DataSource = source;
            ddlKeepDept.DataBind();
            ListItem item = new ListItem();
            item.Text = "All";
            item.Value = "";
            item.Selected = true;
            ddlKeepDept.Items.Add(item);
    
        }
        protected void InitGridView(string Type)
        {
            List<ApplyForm> source = new List<ApplyForm>();
            string Condition = SelectCondition(Type);
            source = CorrectQuery_bll.getCorrecyApplyForm(Condition);
            ViewState["gvsource"] = source;
            bindGridView();
        }

        protected void bindGridView()
        {
            List<ApplyForm> applyform = new List<ApplyForm>();
            applyform = ViewState["gvsource"] as List<ApplyForm>;
            gvInfo.DataSource = applyform;
            gvInfo.DataBind();
        }
        protected string SelectCondition(string type)
        {
            string Condition="";
            if (ddlFasType.SelectedValue != "")
                Condition += " and Fas_Type like '%" + ddlFasType.SelectedValue.ToString() + "%' ";
            if (txtAssetNO.Text != "")
                Condition += " and fas_No like '" + txtAssetNO.Text.ToString() + "'";
            if (ddlKeepDept.SelectedValue != "")
                Condition += " and KeepDept like '%" + ddlKeepDept.SelectedValue.ToString() + "%'";
            if (txtKeepName.Text != "")
                Condition += " and (Keeper like '%" + txtKeepName.Text + "%' or user_id like '%" + txtKeepName.Text + "%')";
            if (txtProductName.Text != "")
                Condition += " and chi_name like '%" + txtProductName.Text + "%'";
            if (txtVendor.Text != "")
                Condition += " and brand like '%" + txtVendor.Text + "%'";
            if (txtSpec.Text != "")
                Condition += " and spec like '%" + txtSpec.Text + "%'";
            if (txtModelNO.Text != "")
                Condition += " and model_no like '%" + txtModelNO.Text + "%'";
            if (ddlNowState.SelectedValue != "")
                Condition += " and status = '" + ddlNowState.SelectedValue + "'";
            if (txtPRNO.Text != "")
                Condition += " and PR_NO like '%" + txtPRNO.Text + "%'";
            if (ddlCheckResult.SelectedValue != "")
                Condition += " and Correct_result = '" + ddlCheckResult.SelectedValue + "'";
            if (type == "QueryCount") //判斷是哪一個Button
                return Condition;
            if (ddlApprovalState.SelectedValue != "")
                Condition += " and FormStatus = '" + ddlApprovalState.SelectedValue + "'";
            return Condition;
        }

        protected void btnQueryDetail_Click(object sender, EventArgs e)
        {
            panGridView.Visible = true;
            panTable.Visible = false;
            InitGridView("QueryDetail");
        }
        protected string getCurrentStatus(ApplyForm form)
        {
            string CurrentStatus = "";
            switch ((AssetStatus)form.Item.CurrentStatus)
            {
                case AssetStatus.NeedCorrect:
                    CurrentStatus = "需校驗";
                    break;
                case AssetStatus.AgainstCorrect:
                    CurrentStatus = "免檢";
                    break;
                case AssetStatus.AlreadyCorrect:
                    CurrentStatus = "已校驗";
                    break;
                case AssetStatus.ForbidUse:
                    CurrentStatus = "禁止使用";
                    break;
                case AssetStatus.HaventCorrect:
                    CurrentStatus = "未校驗";
                    break;
                case AssetStatus.PauseInUse:
                    CurrentStatus = "暫停使用";
                    break;
                case AssetStatus.Useless:
                    CurrentStatus = "報廢";
                    break;
                default:
                    break;
            }
            return CurrentStatus;
        }
        protected string getCorrectResult(ApplyForm form)
        {
            string Result = "";
            switch ((CorrectResult)form.Item.CorrectResult)
            {
                case CorrectResult.Qualified:
                    Result = "合格";
                    break;
                case CorrectResult.RestrictedForUse:
                    Result = "限定使用";
                    break;
                case CorrectResult.Unqualified:
                    Result = "不合格";
                    break;
                default:
                    break;
            }
            return Result;
        }
        protected string getApprovalResult(ApplyForm form)
        {
            string ApprovalResult = "";
            switch ((FormStatus)form.FormStatus)
            {
                case FormStatus.Agree:
                    ApprovalResult = "同意";
                    break;
                case FormStatus.Disagree:
                    ApprovalResult = "不同意";
                    break;
                case FormStatus.Draft:
                    ApprovalResult = "草稿";
                    break;
                case FormStatus.Processing:
                    ApprovalResult = "檢核中";
                    break;
                case FormStatus.WithDraw:
                    ApprovalResult = "抽單";
                    break;
                default:
                    break;
            }
            return ApprovalResult;
        }
        protected string getAttach(ApplyForm form)
        {
            string Attach = "";
            string id = form.Item.AssetNO.ToString();
            Attach += "<a target=\"_blank\" href=\"uploadAttach.aspx?id=" + id + "\" ><img src=\"images\\imgAttachment.gif\" runat=\"server\" border=0 alt=\"附件\"/></a>";
            return Attach;

        }
        protected void gvInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ApplyForm applyform = e.Row.DataItem as ApplyForm;
                e.Row.Cells[0].Text = applyform.Item.AssetNO;
                e.Row.Cells[2].Text = applyform.Item.ChiName.ToString();
                e.Row.Cells[3].Text = applyform.Item.Spec.ToString();
                e.Row.Cells[4].Text = applyform.Creater.EmpChineseName.ToString();
                e.Row.Cells[5].Text = applyform.Creater.DeptNO.ToString();
                e.Row.Cells[6].Text = getCurrentStatus(applyform);
                e.Row.Cells[7].Text = getCorrectResult(applyform);
                e.Row.Cells[8].Text = getAttach(applyform);
                e.Row.Cells[9].Text = getApprovalResult(applyform);

            }
        }
 
        protected void btnQueryCount_Click(object sender, EventArgs e)
        {
            panGridView.Visible = false;
            panTable.Visible = true;
            List<QueryItem> item = new List<QueryItem>();
            string Condition = SelectCondition("QueryCount");
            item = CorrectQuery_bll.StatusCount(Condition);
            gvCount.DataSource = item;
            gvCount.DataBind();

        }
        protected string Percent(int Need, int Aready)
        {
            string percent = "";
            if (Aready == 0)
                percent = "0";
            else
            {
                percent = (Aready / Need * 100).ToString();
                percent += percent + "%";
            }
            return percent;
        }
        protected void gvCount_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {   
                QueryItem item = e.Row.DataItem as QueryItem;
                int Need = item.NeedCorrectNum;
                int Already = item.AlreadyCorrectNum;
                e.Row.Cells[8].Text = Percent(Need, Already);
                if (item.DeptNO == "Total:")
                {
                    e.Row.Attributes.Add("style", "background-Color:blue;");
                    e.Row.Cells[0].Attributes.Add("style", "background-color:red");
                }
            }
 
        }

        protected void gvInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (gvInfo.PageIndex > gvInfo.PageCount)
                gvInfo.PageIndex = gvInfo.PageCount;
            else
                gvInfo.PageIndex = e.NewPageIndex;
            bindGridView();
        }

      

      


    }

}
